<template>
	<view>
		<u-navbar height="50" back-icon-color="#000" :background="background" title="支付" title-color="#000"
			title-size="36">
		</u-navbar>
		<!-- <view class="payTimeView">
			<view class="payTimeTitle">支付剩余时间</view>
			<u-count-down :show-hours="false" :timestamp="timestamp" font-size="40" separator-size="40"
				separator-color="#F6632D" color="#F6632D">
			</u-count-down>
		</view> -->
		<view class="payTimeView2" style="margin-top: 30rpx;">
			<view class="payPrice">￥{{totalPrice}}</view>
			<view class="orderNumView">
				<text class="orderNum">订单号：{{orderNo}}</text>
			</view>
		</view>
		<view class="payItemView">
			<view class="payTypeTitme">请选择支付方式</view>
			<view class="payTypeItem" @click="onSelected(0)">
				<u-icon name="/static/weixin.png" size="50"></u-icon>
				<view class="payTypeTitle">微信</view>
				<u-icon name="/static/selected.png" size="40" v-if="current == 0"></u-icon>
				<u-icon name="/static/unselected2.png" size="40" v-else></u-icon>
			</view>

			<!-- 			<view class="payTypeItem" @click="onSelected(1)">
				<u-icon name="/static/alipay.png" size="50"></u-icon>
				<view class="payTypeTitle">支付宝</view>
				<u-icon name="/static/selected.png" size="40" v-if="current == 1"></u-icon>
				<u-icon name="/static/unselected2.png" size="40" v-else></u-icon>
			</view> -->

			<view class="payTypeItem" @click="onSelected(2)">
				<u-icon name="/static/icons/dfk.png" size="50"></u-icon>
				<view class="payTypeTitle">余额支付({{balance}})
				</view>
				<u-icon name="/static/selected.png" size="40" v-if="current == 2"></u-icon>
				<u-icon name="/static/unselected2.png" size="40" v-else></u-icon>
			</view>
		</view>
		<view class="relaseView" @click="onOrderPayment()">立即支付</view>
	</view>
</template>

<script>
	import {
		getWechatPay,
		getAlipayPay,
		getWechatPayPt,
		getGroupbuyPay,
		getCashPay,
		getUserInfo
	} from '../../api/user.js'
	import {
		toast
	} from '../../utils/common.js';
	export default {
		data() {
			return {
				timestamp: 1800,
				background: {
					backgroundColor: '#fff'
				},
				current: 0,
				items: [{
					value: '微信',
					image: '/static/weixin.png',
					name: '微信支付',
				}, {
					value: '支付宝',
					image: '/static/alipay.png',
					name: '支付宝支付',
				}, {
					value: '余额',
					image: '/static/balance.png',
					name: '余额支付',
				}],
				orderNo: '',
				totalPrice: '0.00',
				balance: ''
			}
		},
		onLoad(res) {
			console.log(res);
			this.orderNo = res.order_no;
			this.totalPrice = res.money;
			this.current = res.current
			this.onUserInfo();
		},
		methods: {
			onUserInfo() {
				let self = this
				getUserInfo().then(res => {
					self.balance = res.data.balance;
				});
			},
			onSelected(index) {
				this.current = index;
			},
			onOrderPayment() {
				if(this.current==2){
					this.miniPayment()
					return
				}
				let self = this;
				if (this.orderNo.indexOf("PT") != -1) {
					getGroupbuyPay({
						order_no: this.orderNo
					}).then(res => {
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: 'MD5',
							paySign: res.data.paySign,
							success: function(res) {
								// success && success(res.data);
								toast('支付成功');
								setTimeout(function() {
									uni.switchTab({
										url: '/pages/home/home'
									});
								}, 500);
							},
							fail: function(err) {
								uni.showToast({
									title: '支付失败',
									icon: 'none',
									duration: 2000
								});
								// fail && fail(err.data);
							},
							complete(ress) {
								// complete && complete(ress.data);
							}
						});
					})
				} else {
					getWechatPay({
						order_no: this.orderNo
					}).then(res => {
						// self.onPayment("wxpay", res.data);
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: 'MD5',
							paySign: res.data.paySign,
							success: function(res) {
								// success && success(res.data);
								toast('支付成功');
								setTimeout(function() {
									uni.navigateBack({
										delta: 1
									});
								}, 500);
							},
							fail: function(err) {
								uni.showToast({
									title: '支付失败',
									icon: 'none',
									duration: 2000
								});
								// fail && fail(err.data);
							},
							complete(ress) {
								// complete && complete(ress.data);
							}
						});
					})
				}

			},
			miniPayment() {
				let self = this;
				uni.showLoading({
					title: "支付中"
				})
				getCashPay({
					order_no: this.orderNo
				}).then(ress => {
					uni.hideLoading()
					if (ress.code == 200) {
						toast("支付成功")
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/home/home'
							})
						}, 500)
					} else {
						toast(ress.message)
					}
				})
			},

		}
	}
</script>

<style>
	@import url('payment.css');

	page {
		background: #f5f5f5;
	}
</style>